package com.jic.market.impl.rest.discountamount.model;

import com.jic.market.common.util.Money;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinhaijun
 * @date: 2020/4/7
 */
@Data
public class DiscountAmountModel implements Serializable {

    private static final long serialVersionUID = -6192021381568889020L;
    private Long id;
    //** skuId */
    private Long skuId;
    //** skuId */
    private String thirdSkuId;
    /** 数量 */
    private Long num;
    /** 优惠单价金额 */
    private BigDecimal discountSingleAmount;
    /** 优惠总金额 */
    private BigDecimal discountTotalAmount;
    /** 互斥活动，未加入叠加的活动 */
    private ExclusiveGroup exclusiveGroup;
    /** 叠加活动，加入叠加的活动，取创建时间最近的规则组 */
    private OverlayGroup overlayGroup;


    @Data
    public static class ExclusiveGroup implements Serializable {
        private static final long serialVersionUID = -325601161384378958L;
        /** 互斥型优惠单价，取各个活动优惠金额最大值，优惠力度最大 */
        private BigDecimal exclusivePromotionDiscountSingleAmount;
        /** 互斥型优惠总价，取各个活动优惠金额最大值，优惠力度最大 */
        private BigDecimal exclusivePromotionDiscountTotalAmount;
        private List<PromotionAmountModel> promotionAmountModelList;
    }

    @Data
    public static class OverlayGroup implements Serializable {
        private static final long serialVersionUID = 5673007318217823413L;
        /* 叠加规则id */
        private String overlayId;
        /* 叠加规则组id */
        private String overlayGroupId;
        /** 满额减： 0不设置，先将活动以优先级进行排序（对于排序方式，如果遇到两个活动是一个类型则按满额减处理），然后再依次以上一个活动的计算结果代入到下一个活动计算，对于排序方式，如果遇到两个活动是
         *          1设置满额减，每次计算都按照原价计算，也就是不存在优先级的问题
         */
        private String fullAmountReduceFlag;
        /** 叠加型优惠单价，取各个活动优惠金额之和 */
        private BigDecimal overlayPromotionDiscountSingleAmount;
        /** 叠加型优惠总价，取各个活动优惠金额之和 */
        private BigDecimal overlayPromotionDiscountTotalAmount;
        private List<PromotionAmountModel> promotionAmountModelList;
    }


    public static DiscountAmountModel init(Long skuId){
        DiscountAmountModel discountAmountModel = new DiscountAmountModel();
        DiscountAmountModel.ExclusiveGroup exclusiveGroup = new DiscountAmountModel.ExclusiveGroup();
        DiscountAmountModel.OverlayGroup overlayGroup = new DiscountAmountModel.OverlayGroup();
        List<PromotionAmountModel> exclusivePromotionAmountModelList = new ArrayList<>();
        List<PromotionAmountModel> overlayPromotionAmountModelList = new ArrayList<>();
        discountAmountModel.setSkuId(skuId);
        exclusiveGroup.setExclusivePromotionDiscountSingleAmount(BigDecimal.ZERO);
        exclusiveGroup.setExclusivePromotionDiscountTotalAmount(BigDecimal.ZERO);
        exclusiveGroup.setPromotionAmountModelList(exclusivePromotionAmountModelList);
        overlayGroup.setOverlayPromotionDiscountSingleAmount(BigDecimal.ZERO);
        overlayGroup.setOverlayPromotionDiscountTotalAmount(BigDecimal.ZERO);
        overlayGroup.setPromotionAmountModelList(overlayPromotionAmountModelList);
        discountAmountModel.setExclusiveGroup(exclusiveGroup);
        discountAmountModel.setOverlayGroup(overlayGroup);
        return discountAmountModel;
    }

    public static void maxDiscount(DiscountAmountModel discountAmountModel){
        ExclusiveGroup exclusiveGroup = discountAmountModel.getExclusiveGroup();
        OverlayGroup overlayGroup = discountAmountModel.getOverlayGroup();
        //互斥和叠加取最大优惠金额
        if(exclusiveGroup.getExclusivePromotionDiscountSingleAmount().compareTo(overlayGroup.getOverlayPromotionDiscountSingleAmount()) == 1){
            discountAmountModel.setDiscountSingleAmount(exclusiveGroup.getExclusivePromotionDiscountSingleAmount());
            discountAmountModel.setDiscountTotalAmount(exclusiveGroup.getExclusivePromotionDiscountTotalAmount());
            discountAmountModel.setOverlayGroup(null);
        }else if(exclusiveGroup.getExclusivePromotionDiscountSingleAmount().compareTo(overlayGroup.getOverlayPromotionDiscountSingleAmount()) == -1) {
            discountAmountModel.setDiscountSingleAmount(overlayGroup.getOverlayPromotionDiscountSingleAmount());
            discountAmountModel.setDiscountTotalAmount(overlayGroup.getOverlayPromotionDiscountTotalAmount());
            discountAmountModel.setExclusiveGroup(null);
        }else {
            // 如果存在赠送类的活动，则保留它
            List<PromotionAmountModel> exclusivePromotionList = exclusiveGroup.getPromotionAmountModelList();
            Boolean baoliuexclusiveGroup = false;
            if(null != exclusivePromotionList && !exclusivePromotionList.isEmpty()){
                for (PromotionAmountModel exclusivePromotion : exclusivePromotionList) {
                    if(!exclusivePromotion.getDeleteModel()){
                        baoliuexclusiveGroup = true;
                    }
                }
            }

            List<PromotionAmountModel> overlayPromotionList = overlayGroup.getPromotionAmountModelList();
            Boolean baoliuoverlayGroup = false;
            if(null != exclusivePromotionList && !exclusivePromotionList.isEmpty()){
                for (PromotionAmountModel overlayPromotion : overlayPromotionList) {
                    if(!overlayPromotion.getDeleteModel()){
                        baoliuoverlayGroup = true;
                    }
                }
            }

            if(!baoliuexclusiveGroup && !baoliuoverlayGroup){
                discountAmountModel.setDiscountSingleAmount(BigDecimal.ZERO);
                discountAmountModel.setDiscountTotalAmount(BigDecimal.ZERO);
                discountAmountModel.setOverlayGroup(null);
                discountAmountModel.setExclusiveGroup(null);
            }

            if(baoliuexclusiveGroup){
                discountAmountModel.setDiscountSingleAmount(exclusiveGroup.getExclusivePromotionDiscountSingleAmount());
                discountAmountModel.setDiscountTotalAmount(exclusiveGroup.getExclusivePromotionDiscountTotalAmount());
                discountAmountModel.setOverlayGroup(null);
            }

            if(baoliuoverlayGroup){
                discountAmountModel.setDiscountSingleAmount(overlayGroup.getOverlayPromotionDiscountSingleAmount());
                discountAmountModel.setDiscountTotalAmount(overlayGroup.getOverlayPromotionDiscountTotalAmount());
                discountAmountModel.setExclusiveGroup(null);
            }

        }
    }

    public static void maxDiscount(ExclusiveGroup exclusiveGroup){
        List<PromotionAmountModel> promotionAmountModelList = exclusiveGroup.getPromotionAmountModelList();
        BigDecimal maxDiscountAmount = BigDecimal.ZERO;
        PromotionAmountModel maxPromotionAmountModel = null;
        List<PromotionAmountModel> notDeletePromotionAmountModel = new ArrayList<>();

        BigDecimal sumPromotionDiscountSingleAmount = BigDecimal.ZERO;
        BigDecimal sumPromotionDiscountTotalAmount = BigDecimal.ZERO;
        for(PromotionAmountModel promotionAmountModel : promotionAmountModelList){
            if(null == maxPromotionAmountModel){
                maxPromotionAmountModel = promotionAmountModel;
            }
            if(promotionAmountModel.getPromotionDiscountSingleAmount().compareTo(maxDiscountAmount) == 1){
                maxPromotionAmountModel = promotionAmountModel;
                sumPromotionDiscountSingleAmount = promotionAmountModel.getPromotionDiscountSingleAmount();
                sumPromotionDiscountTotalAmount = promotionAmountModel.getPromotionDiscountTotalAmount();
            }
            maxDiscountAmount = promotionAmountModel.getPromotionDiscountSingleAmount();
            if(!promotionAmountModel.getDeleteModel()){
                notDeletePromotionAmountModel.add(promotionAmountModel);
            }
        }
        exclusiveGroup.getPromotionAmountModelList().clear();
        if(!notDeletePromotionAmountModel.isEmpty()){
            exclusiveGroup.getPromotionAmountModelList().add(notDeletePromotionAmountModel.get(0));
        }else {
            exclusiveGroup.getPromotionAmountModelList().add(maxPromotionAmountModel);
        }
        exclusiveGroup.setExclusivePromotionDiscountSingleAmount(sumPromotionDiscountSingleAmount);
        exclusiveGroup.setExclusivePromotionDiscountTotalAmount(sumPromotionDiscountTotalAmount);
    }

    public static void sumDiscount(OverlayGroup overlayGroup){
        List<PromotionAmountModel> promotionAmountModelList =  overlayGroup.getPromotionAmountModelList();
        BigDecimal sumPromotionDiscountSingleAmount = BigDecimal.ZERO;
        BigDecimal sumPromotionDiscountTotalAmount = BigDecimal.ZERO;
        for(PromotionAmountModel promotionAmountModel : promotionAmountModelList){
            sumPromotionDiscountSingleAmount = Money.add(sumPromotionDiscountSingleAmount, promotionAmountModel.getPromotionDiscountSingleAmount());
            sumPromotionDiscountTotalAmount = Money.add(sumPromotionDiscountTotalAmount, promotionAmountModel.getPromotionDiscountTotalAmount());
        }
        overlayGroup.setOverlayPromotionDiscountSingleAmount(sumPromotionDiscountSingleAmount);
        overlayGroup.setOverlayPromotionDiscountTotalAmount(sumPromotionDiscountTotalAmount);
    }

}
